home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWNotifn / Sources / FWRecevr.cpp < prev   
Encoding:
Text File  |  1996-08-16  |  4.9 KB  |  173 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRecevr.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWRECEVR_H
  13. #include "FWRecevr.h"
  14. #endif
  15.  
  16. #ifndef FWNOTIFR_H
  17. #include "FWNotifr.h"
  18. #endif
  19.  
  20. #ifndef FWINTERE_H
  21. #include "FWIntere.h"
  22. #endif
  23.  
  24. //======================================================================================
  25. // Interest Matching Proc
  26. //======================================================================================
  27.  
  28. static FW_Boolean FW_InterestMatchProc(const void* v1, const void* v2)
  29. {
  30.     FW_CInterest* i1 = (FW_CInterest*) v1;
  31.     FW_CInterest* i2 = (FW_CInterest*) v2;
  32.  
  33.     return (*i1 == *i2);
  34. }
  35.  
  36. //========================================================================================
  37. // CLASS FW_MReceiver
  38. //========================================================================================
  39.  
  40. FW_DEFINE_CLASS_M0(FW_MReceiver)
  41. FW_DEFINE_AUTO(FW_MReceiver)
  42.  
  43. //----------------------------------------------------------------------------------------
  44. // FW_MReceiver::FW_MReceiver
  45. //----------------------------------------------------------------------------------------
  46.  
  47. FW_MReceiver::FW_MReceiver() :
  48.     fIsConnected(TRUE),
  49.     fInterestList(FW_InterestMatchProc)
  50. {
  51.     FW_END_CONSTRUCTOR
  52. }
  53.  
  54. //----------------------------------------------------------------------------------------
  55. // FW_MReceiver::~FW_MReceiver
  56. //----------------------------------------------------------------------------------------
  57.  
  58. FW_MReceiver::~FW_MReceiver()
  59. {
  60.     FW_START_DESTRUCTOR
  61.     
  62.     RemoveAllInterests();    
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // FW_MReceiver::AddInterest
  67. //----------------------------------------------------------------------------------------
  68.  
  69. void FW_MReceiver::AddInterest(const FW_CInterest& interest)
  70. {
  71.     // Check that the interest is not already registered
  72.     if (fInterestList.Contains(&interest))
  73.         return;
  74.     
  75.     FW_CInterest* interestCopy = new FW_CInterest(interest);
  76.     
  77.     if (fIsConnected)
  78.         interestCopy->GetNotifier()->AddReceiver(this, interestCopy);
  79.  
  80.     fInterestList.AddLast(interestCopy);
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_MReceiver::RemoveAllInterests
  85. //----------------------------------------------------------------------------------------
  86.  
  87. void FW_MReceiver::RemoveAllInterests()
  88. {
  89.     FW_CInterest* anInterest;
  90.     
  91.     if (fIsConnected)
  92.     {
  93.         FW_TOrderedCollectionIterator<FW_CInterest> ite(&fInterestList);
  94.         for (anInterest = ite.First(); ite.IsNotComplete(); anInterest = ite.Next())
  95.         {
  96.             anInterest->GetNotifier()->RemoveReceiver(this, *anInterest);
  97.         }
  98.     }
  99.  
  100.     while ((anInterest = fInterestList.First()) != NULL)
  101.     {
  102.         fInterestList.Remove(anInterest);
  103.         delete anInterest;
  104.     }
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // FW_MReceiver::RemoveInterest
  109. //----------------------------------------------------------------------------------------
  110.  
  111. void FW_MReceiver::RemoveInterest(const FW_CInterest& interest)
  112. {    
  113.     FW_TOrderedCollection<FW_CInterest> temp;
  114.     
  115.     FW_CInterest* anInterest;
  116.     
  117.     FW_TOrderedCollectionIterator<FW_CInterest> ite1(&fInterestList);
  118.     for (anInterest = ite1.First(); ite1.IsNotComplete(); anInterest = ite1.Next())
  119.     {
  120.         if (*anInterest == interest)
  121.             temp.AddLast(anInterest);
  122.     }
  123.     
  124.     FW_TOrderedCollectionIterator<FW_CInterest> ite2(&temp);
  125.     for (anInterest = ite2.First(); ite2.IsNotComplete(); anInterest = ite2.Next())
  126.     {
  127.         fInterestList.Remove(anInterest);
  128.         
  129.         if (fIsConnected)
  130.             anInterest->GetNotifier()->RemoveReceiver(this, *anInterest);
  131.             
  132.         delete anInterest;
  133.     }
  134.     
  135.     // [HLX] no need to remove from temp. FW_CInterestCollection::__dt calls RemoveAll
  136. }
  137.     
  138. //----------------------------------------------------------------------------------------
  139. // FW_MReceiver::Connect
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void FW_MReceiver::Connect()
  143. {
  144.     if (!fIsConnected)
  145.     {
  146.         FW_TOrderedCollectionIterator<FW_CInterest> ite(&fInterestList);
  147.         for (FW_CInterest* anInterest = ite.First(); ite.IsNotComplete(); anInterest = ite.Next())
  148.         {
  149.             anInterest->GetNotifier()->AddReceiver(this, anInterest);
  150.         }
  151.     }
  152.     
  153.     fIsConnected = TRUE;
  154. }
  155.                                
  156. //----------------------------------------------------------------------------------------
  157. // FW_MReceiver::Disconnect
  158. //----------------------------------------------------------------------------------------
  159.  
  160. void FW_MReceiver::Disconnect()
  161. {
  162.     if (fIsConnected)
  163.     {
  164.         FW_TOrderedCollectionIterator<FW_CInterest> ite(&fInterestList);
  165.         for (FW_CInterest* anInterest = ite.First(); ite.IsNotComplete(); anInterest = ite.Next())
  166.         {
  167.             anInterest->GetNotifier()->RemoveReceiver(this, *anInterest);
  168.         }
  169.     }
  170.     
  171.     fIsConnected = FALSE;
  172. }
  173.